Load NeuronChat library

library(NeuronChat)
#> Loading required package: dplyr
#> Warning: package 'dplyr' was built under R version 4.1.2
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> Loading required package: igraph
#> Warning: package 'igraph' was built under R version 4.1.2
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:dplyr':
#> 
#>     as_data_frame, groups, union
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
#> Loading required package: ggplot2
#> Warning: package 'ggplot2' was built under R version 4.1.2
library(CellChat)

Part I: Create NeuronChat object

# load example data: cortex data from Yao 2021, processed
data(list='cortex_data')
# subset the data by choosing the region VISp
region_name <- 'VISp'; cell_class <- names(table(meta$class_label))
cell_idx <- which(meta$region_label %in% region_name & meta$class_label %in% cell_class & !(meta$subclass_label %in%c('Car3','CR','DG','L2/3 IT PPP','L5/6 IT TPE-ENT')))
target_df_single  <- target_df[cell_idx,] # a data frame: row  cell, column gene (the last column is the cell subclass)
meta_tmp <- meta[cell_idx,];rownames(meta_tmp) <- meta_tmp$sample_name # subset meta
df_group <- meta_tmp[!duplicated(meta_tmp$subclass_label),c('class_label','subclass_label')]
group <- structure(df_group$class_label,names=df_group$subclass_label) # create the cell class for the subclasses, used for plot 

# creat NeuronChat object; choose the database 'mouse' for mouse data; 'human' for human data
# note that the first parameter should be a numeric matrix with row gene and column cell
x <- createNeuronChat(t(as.matrix(target_df_single[,1:(dim(target_df_single)[2]-1)])),DB='mouse',group.by = target_df_single$cell_subclass);
#> Create a NeuronChat object from a data matrix

Part II: Run NeuronChat to infer the neural-specific cell-cell communication networks

# M is for the permutation test; typically ~4 mins when M=100, depending on the dataset size and the number of cell groups
# setting M=10 to get a quick start
x <- run_NeuronChat(x,M=2)
#> Time difference of 10.14619 secs
# the the communication networks for individual interaction pairs are stored in slot 'net'
# aggregate the communication networks over all interaction pairs, method can be 'weight', 'count' and so on
net_aggregated_x <- net_aggregation(x@net,method = 'weight')

Part III: Visualization of neural-specific cell-cell communication networks

for aggregated network

par(mfrow=c(1,2))
# Visualization, circle plot, for the aggregated network
netVisual_circle_neuron(net_aggregated_x,group=group,vertex.label.cex = 1)
# Visualization, chordDiagram, for the aggregated network; also using cellchat function netVisual_chord_cell_internal(net_aggregated_x, group = group,lab.cex=1)
netVisual_chord_neuron(x,method = 'weight',group=group,lab.cex = 1)

# Visualization, heatmap for the aggregated network
heatmap_aggregated(x, method='weight',group=group)

for individual network

par(mfrow=c(1,2))
# Visualization for the single interaction pair, circle plot  
netVisual_circle_neuron(x@net$Glu_Gria2,group=group,vertex.label.cex = 1)
# Visualization for the single interaction pair, chord diagram 
netVisual_chord_neuron(x,interaction_use='Glu_Gria2',group=group,lab.cex = 1)

# Visualization for the single interaction pair, heatmap 
heatmap_single(x,interaction_name='Glu_Gria2',group=group)

# Visualization for the single interaction pair, heatmap with violin plots showing expression of genes realted to ligand and target
lig_tar_heatmap(x,interaction_name='Glu_Gria2',width.vector=c(0.38,0.35,0.27))

Part IV: Analysis of communication

outgoing/incoming pattern

# selectK_Neuron(x,pattern = "outgoing")
# selectK_Neuron(x,pattern = "incoming")
x<- identifyCommunicationPatterns_Neuron(x, slot.name = "net", pattern = c("outgoing"), k=4,height = 18)
x<- identifyCommunicationPatterns_Neuron(x, slot.name = "net", pattern = c("incoming"), k=4,height = 18)

library(ggalluvial)
netAnalysis_river_Neuron(x,slot.name = "net", pattern = c("outgoing"),font.size = 2.5,cutoff.1 = 0.5,cutoff.2=0.5)
#> Please make sure you have load `library(ggalluvial)` when running this function

netAnalysis_river_Neuron(x,slot.name = "net", pattern = c("incoming"),font.size = 2.5,cutoff.1 = 0.5,cutoff.2=0.5)
#> Please make sure you have load `library(ggalluvial)` when running this function

manifold learning and classification

x <- computeNetSimilarity_Neuron(x,type='functional')
x  <- CellChat::netEmbedding(x, slot.name = "net_analysis", type = "functional")
#> Manifold learning of the signaling networks for a single dataset
x <- CellChat::netClustering(x, type='functional',slot.name = "net_analysis",k=5)
#> Classification learning of the signaling networks for a single dataset
netVisual_embedding_Neuron(x, type = "functional", label.size = 5,pathway.remove.show = F)

netVisual_embeddingZoomIn_Neuron(x, type = "functional", nCol = 2,label.size = 3)